package org.speed.dubbo;

import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.TimeUnit;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.URLBuilder;
import org.apache.dubbo.common.bytecode.NoSuchMethodException;
import org.apache.dubbo.common.bytecode.Wrapper;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.timer.HashedWheelTimer;
import org.apache.dubbo.common.timer.Timeout;
import org.apache.dubbo.common.timer.TimerTask;
import org.apache.dubbo.common.utils.NamedThreadFactory;
import org.apache.dubbo.rpc.Exporter;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Protocol;
import org.apache.dubbo.rpc.ProxyFactory;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcInvocation;
import org.speed.dubbo.model.Order;
import org.speed.dubbo.provider.OrderServiceImpl;
import org.speed.dubbo.service.OrderService;

public class SPIMain {

	

	public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException {

		//ProxyFactory PROXY_FACTORY = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
		//System.out.println(PROXY_FACTORY.toString());
		
		
		OrderService orderService = new OrderServiceImpl();
		//Wrapper wrapper = Wrapper.getWrapper(orderService.getClass().getName().indexOf('$') < 0 ? orderService.getClass() : OrderService.class);
			
	
		//Class<?>[] parameterTypes = new Class<?>[]{Order.class,String.class};
		//Object[] arguments = new Object[] {new Order(),"2"};
		//wrapper.invokeMethod(orderService, "order", types, arg);
		//injvm://127.0.0.1/org.speed.dubbo.service.OrderService?accesslog=true&anyhost=true&application=speed-dubbo-provider&bean.name=org.speed.dubbo.service.OrderService&bind.ip=192.168.0.101&bind.port=20890&delay=-1&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=org.speed.dubbo.service.OrderService&methods=order&pid=28106&register=true&release=2.7.3&side=provider&timestamp=1593332944458
		//ProxyFactory PROXY_FACTORY = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
        //URL url = new URL("dubbo","127.0.0.1?server=11&interface=org.speed.dubbo.service.OrderService",8080);
		//Invoker<?> invoker = PROXY_FACTORY.getInvoker(orderService, (Class) OrderService.class,url );
		
		//RpcInvocation invocation = new RpcInvocation();
		//invocation.setMethodName("order");
		//invocation.setParameterTypes(parameterTypes);
		//invocation.setArguments(arguments);
		//Object res;
		try {
			//res = invoker.invoke(invocation).recreate();
			 //System.out.println(res);
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
       
		
		
		Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
		ProxyFactory PROXY_FACTORY = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();

		String LOCAL_PROTOCOL = "injvm";
		String LOCALHOST_VALUE = "127.0.0.1";
		
		//dubbo://192.168.0.101:20890/org.speed.dubbo.service.OrderService?accesslog=true&anyhost=true&application=speed-dubbo-provider&bean.name=org.speed.dubbo.service.OrderService&bind.ip=192.168.0.101&bind.port=20890&delay=-1&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=org.speed.dubbo.service.OrderService&methods=order&pid=28527&register=true&release=2.7.3&side=provider&timestamp=1593334357567
		URL url = new URL("dubbo","127.0.0.1?server=11&interface=org.speed.dubbo.service.OrderService",8080);
		URL local = URLBuilder.from(url)
                .setProtocol(LOCAL_PROTOCOL)
                .setHost(LOCALHOST_VALUE)
                .setPort(0)
                .build();
		
		
		A a = new A();
		
		

        Invoker proxy = PROXY_FACTORY.getInvoker(orderService, (Class) OrderService.class, local);
        
        a.export(proxy);
        
        //System.out.println(proxy.getUrl().toFullString());
        //Exporter<?> exporter = protocol.export(proxy);
        
        System.out.println("ok");
        
        
	}

	private static void timeout() {
		int TICKS_PER_WHEEL = 128;
		HashedWheelTimer IDLE_CHECK_TIMER = new HashedWheelTimer(new NamedThreadFactory("dubbo-server-idleCheck", true), 1,
	            TimeUnit.SECONDS, TICKS_PER_WHEEL);
		
		IDLE_CHECK_TIMER.newTimeout(new TimerTask() {
			
			@Override
			public void run(Timeout timeout) throws Exception {
				
				System.out.println("time out!");
				
			}
		}, 5, TimeUnit.SECONDS);
	}

}
